#include <boost/shared_ptr.hpp>
#include <iostream>
#include <map>

using namespace boost;
using namespace std;

class Data
{
public:
	Data(int v):data_(v){cout<<"Data("<< data_ << ")"<<endl;}
	~Data(){cout<<"~Data(" << data_ <<")"<<endl;}
	Data(const Data& data){cout<<"copy Data"<<endl;data_ = data.data_;}
	Data& operator = (const Data& data){cout<<"= operator"<<endl; data_ = data.data_;}

private:
	int data_;
};

class Key
{
public:
	Key(int v):data_(v){cout<<"key("<< data_ << ")"<<endl;}
	~Key(){cout<<"~Key(" << data_ <<")"<<endl;}
	Key(const Key& data){cout<<"copy Key"<<endl;data_ = data.data_;}
	Key& operator = (const Key& data){cout<<"= operator"<<endl; data_ = data.data_;}
private:
	int data_;
};

typedef shared_ptr<Key> KeyPtr;
typedef shared_ptr<Data> DataPtr;
typedef map<KeyPtr, DataPtr> DataMap;

DataMap dmap;

void add(KeyPtr& key , DataPtr& data)
{
	dmap[key] = data;
}

void test(DataPtr& data)
{
	cout << "void test(DataPtr& data)" <<endl;
	if(data.unique())
		cout << "data is unique"<<endl;
	else
		cout << "data is multi-pointer"<<endl;
	cout << "data.use_count = "<< data.use_count() << endl;
}

void test2(DataPtr data)
{
	cout << "void test2(DataPtr data)" <<endl;
	if(data.unique())
		cout << "data is unique"<<endl;
	else
		cout << "data is multi-pointer"<<endl;
	cout << "data.use_count = "<< data.use_count() << endl;
}


int main()
{
	{
	KeyPtr key1(new Key(1));
	KeyPtr key2(new Key(2));
	KeyPtr key3(new Key(3));


	DataPtr data1(new Data(100));
	DataPtr data2(new Data(200));
	DataPtr data3(new Data(300));

	add(key1, data1);
	add(key2, data2);
	add(key3, data3);
	}

	cout<<"map size: "<< dmap.size() << endl;

	DataMap::iterator it = dmap.begin();
	if(it != dmap.end())
	{
		KeyPtr key = it->first;
		DataPtr data = it->second;
//		cout<<"data.use_count = "<<data.use_count()<<endl;
//		test(data);
//		test2(data);
		dmap.erase(key);
	}

	cout<<" size of dmap: "<< dmap.size() <<endl;
	cout<<"end"<<endl;


	
}
